using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gj
{
    public class TimerHelper : NodeHelper
    {
        public float time;
        public BindDataType dataType = BindDataType.Bool;
        public EaseType easeType = EaseType.linear;
        protected override BindDataType bindDataType => dataType;
        private float _time;
        private float _result;
        private bool _open;
        private float _value;
        private bool _ing;

        protected override void BindData(bool b)
        {
            if (_open == b) return;
            _open = b;
            if (_open)
            {
                _time = Time.time;
                _result = 0;
                _ing = true;
            }
            else
            {
                _ing = false;
            }
        }

        protected override void BindData(float f)
        {
            if (_value == f) return;
            _value = f;
            _time = Time.time;
            time = f - _time;
            _result = 0;
            _ing = true;
        }

        private void Update()
        {
            if (!_ing) return;

            if (_result == 1)
            {
                _ing = false;
                Emit(UIEvent.End);
                return;
            }
            _result = MathTools.Compute(easeType, 0, 1, (Time.time - _time) / time);
            Change(_result);
        }
    }
}